package day19;

public class Demo1 {
    private volatile static int a;
    private volatile static int b;
    private volatile static int c;
    private volatile static int d;
    public static void main(String[] args) throws InterruptedException {
        int i = 0;
        while (true) {
            //统计次数
            i++;
            a=b=c=d=0;
            Thread t1 = new Thread(()->{
               a=1;
               c=b;
               //正常来说先执行a=1,后c=b;
                //但是重排序后  可能先执行c=b;
                //会导致c=0;
                //如果没有重排序
            });
            Thread t2 = new Thread(()->{
                b=1;
                d=a;
                //也会导致d=0
            });
            t1.start();
            t2.start();
            t1.join();
            t2.join();
            if(c==0&&d==0){
                System.out.println("执行了多少重排序次数:"+i);
                break;
            }
            else {
                System.out.println(i);
            }
        }
    }
}
